package com.bym.mall.listener;

import cn.hutool.json.JSONUtil;
import com.bym.mall.entity.PayInfo;
import com.bym.mall.service.IOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * rabbitmq支付消息监听器
 *
 * @author My
 * @date 2022/06/25
 */
@Slf4j
@RabbitListener(queues = "myQueue")
@Component
public class PayMsgListener {

    @Autowired
    private IOrderService orderService;

    @RabbitHandler
    public void process(String msg) {
        log.info("receive msg from rabbitmq:{}", msg);

        PayInfo payInfo = JSONUtil.toBean(msg, PayInfo.class);
        if ("SUCCESS".equals(payInfo.getPlatformStatus())) {
            //修改订单状态，未支付->已支付
            orderService.paid(payInfo.getOrderNo());
        }
    }
}
